#!/usr/bin/env bash

# shellcheck disable=SC2015

# Useful to run queries in parallel sessions
function tx()
{
    tx_num=$1
    query=$2

    session="${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}_tx$tx_num"
    query_id="${session}_${RANDOM}"
    url_without_session="https://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT_HTTPS}/?"
    url="${url_without_session}session_id=$session&query_id=$query_id&database=$CLICKHOUSE_DATABASE"

    ${CLICKHOUSE_CURL} -m 90 -sSk "$url" --data "$query" | sed "s/^/tx$tx_num\t/"
}

# Waits for the last query in session to finish
function tx_wait() {
    tx_num=$1

    session="${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}_tx$tx_num"

    # try get pid of previous query
    query_pid=""
    tmp_file_name="${CLICKHOUSE_TMP}/tmp_tx_${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}"
    query_id_and_pid=$(grep -F "$session" "$tmp_file_name" 2>/dev/null | tail -1) ||:
    read -r query_id query_pid <<< "$query_id_and_pid" ||:

    # wait for previous query in transaction
    if [ -n "$query_pid" ]; then
        timeout 5 tail --pid=$query_pid -f /dev/null && return ||:
    fi

    # there is no pid (or maybe we got wrong one), so wait using system.processes (it's less reliable)
    count=0
    while [[ $($CLICKHOUSE_CLIENT -q "SELECT count() FROM system.processes WHERE query_id LIKE '$session%'") -gt 0 ]]; do
        sleep 0.5
        count=$((count+1))
        if [ "$count" -gt 120 ]; then
            echo "timeout while waiting for $tx_num"
            break
        fi
    done;
}

# Wait for previous query in session to finish, starts new one asynchronously
function tx_async()
{
    tx_num=$1
    query=$2

    tx_wait "$tx_num"

    session="${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}_tx$tx_num"
    query_id="${session}_${RANDOM}"
    url_without_session="https://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT_HTTPS}/?"
    url="${url_without_session}session_id=$session&query_id=$query_id&database=$CLICKHOUSE_DATABASE"

    # We cannot be sure that query will actually start execution and appear in system.processes before the next call to tx_wait
    # Also we cannot use global map in bash to store last query_id for each tx_num, so we use tmp file...
    tmp_file_name="${CLICKHOUSE_TMP}/tmp_tx_${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}"

    # run query asynchronously
    ${CLICKHOUSE_CURL} -m 90 -sSk "$url" --data "$query" | sed "s/^/tx$tx_num\t/" &
    query_pid=$!
    echo -e "$query_id\t$query_pid" >> "$tmp_file_name"
}

# Wait for previous query in session to finish, execute the next one synchronously
function tx_sync()
{
    tx_num=$1
    query=$2
    tx_wait "$tx_num"
    tx "$tx_num" "$query"
}

# vi: ft=bash
